import unittest,requests


class MyTestCase(unittest.TestCase):
    # todo 登录
    token=None
    def test_01_login(self):
        url='http://10.59.9.14:7003/api/public/login'
        json={"username":"15517630150","password":"123456"}
        login=requests.post(url=url,json=json)
        print(login.json())
        MyTestCase.token=login.json()['data']['token']
        print(MyTestCase.token)
    #     todo 秒杀列表
    spid=None
    def test_02_list(self):
        url='http://10.59.9.14:7003/api/second?page=1&type=0&sort=1'
        mslist=requests.get(url=url)
        print(mslist.json())
        MyTestCase.spid=mslist.json()['data']['list'][1]['id']
        print(MyTestCase.spid)
#    todo   商品详情
    def test_03_detail(self):
        url = 'http://10.59.9.14:7003/api/second/view?id={}'.format(MyTestCase.spid)
        detail = requests.get(url=url)
        print(detail.json())
#   todo 立即购买
    def test_04_confirm(self):
        url='http://10.59.9.14:7003/api/order/confirm?id={}&type=second&sku_id=&num=1&shipping_type=1'.format(MyTestCase.spid)
        headers={"Auth-Token":"Bearer "+MyTestCase.token}
        confirm=requests.get(url=url,headers=headers)
        print(confirm.json())
#   todo 创建订单
    order_id=None
    def test_05_create(self):
        url='http://10.59.9.14:7003/api/order/create'
        json={"id":MyTestCase.spid,"type":"second","address_id":178,"store_id":"0","shipping_type":1,"bonus_id":"0","payment":"balance","remarks":"","source":1,"url":"http://10.59.9.14:7003/wap/pages/cart/confirm?id=15&sku_id=&num=1&type=second","sku_id":"","num":"1"}
        headers = {"Auth-Token": "Bearer " + MyTestCase.token}
        create=requests.post(url=url,json=json,headers=headers)
        print(create.json())
        MyTestCase.order_id=create.json()['data']['order_id']
#   todo 订单信息
    def test_06_detail(self):
        url='http://10.59.9.14:7003/api/order/detail'
        json={"id":MyTestCase.order_id}
        headers = {"Auth-Token": "Bearer " + MyTestCase.token}
        detail=requests.post(url=url,json=json,headers=headers)
        print(detail.json())


if __name__ == '__main__':
    unittest.main()
